#include "../include/server.h"
int init_socket(char *ip, char *port)
{
    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(sfd, -1, "socket");
    struct sockaddr_in saddr;
    saddr.sin_addr.s_addr = inet_addr(ip);
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(atoi(port));
    int reuse = 1;
    setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int));
    int ret = bind(sfd, (struct sockaddr *)&saddr, sizeof(struct sockaddr));
    ERROR_CHECK(ret, -1, "bind");
    ret = listen(sfd, MAXCON);
    ERROR_CHECK(ret, -1, "listen");
    return sfd;
}
MYSQL *init_mysql()
{
    MYSQL *mysql_con;
    MYSQL_RES *res;          // sql执行结果
    MYSQL_ROW row;           //保存行数据
    unsigned int num_fields; //保存列数据
    mysql_con = mysql_init(NULL);
    if (mysql_con == NULL)
    {
        printf("mysql_init failed\n");
        return NULL;
    }
    mysql_con = mysql_real_connect(mysql_con, "localhost", "root", "XQH211yy", "project", 3306, NULL, 0);
    if (mysql_con == NULL)
    {
        printf("mysql_con==NULL\n");
        return NULL;
    }
    return mysql_con;
}
int client_login(MYSQL *mysql_con, int sfd, int *usr_id)
{
    openlog("login", LOG_CONS | LOG_ODELAY | LOG_PID, LOG_USER);
    Train train;
    memset(&train, 0, sizeof(Train));
    int login_flag;
    recvn(sfd, &train.len, 4);
    recvn(sfd, &login_flag, train.len);
    time_t t1;
    if (login_flag == -1)
    {
        return F_ret;
    }
    char name[20] = {0}, passwd[50] = {0};
    recvn(sfd, &train.len, 4);
    recvn(sfd, name, train.len);
    printf("name=%s\n", name);

    memset(&train, 0, sizeof(Train));
    int ret = recvn(sfd, &train.len, 4);
    ret = recvn(sfd, passwd, train.len);
    printf("recv passwd=%s\n", passwd);

    //查找数据库密码和输入的密码是否相同，还需要发回正确与否标志信息
    if (login_flag == 1)
    {
        ret = add_mysql_usr(mysql_con, name, passwd, usr_id);
        ret = usr_fat_init(mysql_con, *usr_id, name);
        if (ret == F_ret)
        {
            del_mysql_usr(mysql_con, *usr_id);
        }
    }
    else if (login_flag == 2)
    {
        ret = select_mysql_usr(mysql_con, name, passwd, usr_id);
    }
    if (ret == F_ret)
    {
        ret = 0;
        train.len = sizeof(int);
        memcpy(train.buf, &ret, train.len);
        sendn(sfd, &train, 4 + train.len);
        syslog(LOG_INFO, "LOG INFO:login fail usr_id:%d time:%s", *usr_id, ctime(&t1));
        return F_ret;
    }

    //发送成功标志
    //在虚拟文件表添加一行
    memset(&train, 0, sizeof(train));
    train.len = sizeof(int);
    ret = 1;
    memcpy(train.buf, &ret, train.len);
    sendn(sfd, &train, 4 + train.len);
    syslog(LOG_INFO, "LOG INFO:login success usr_id:%d time:%s", *usr_id, ctime(&t1));
    return S_ret;
}
void init_thread_pool(Thread_Pool *thread_pool, char *num)
{
    bzero(thread_pool, sizeof(Thread_Pool));
    thread_pool->pthread_num = atoi(num);
    thread_pool->exit_flag = 0;
    thread_pool->ptid = malloc(sizeof(pthread_t) * thread_pool->pthread_num);
    bzero(&thread_pool->tqueue, sizeof(Task_Queue));
    pthread_mutex_init(&thread_pool->tqueue.mutex, NULL);
    pthread_cond_init(&thread_pool->tqueue.cond, NULL);
}
void create_thread(Thread_Pool *thread_pool, int num)
{
    for (int i = 0; i < num; i++)
    {
        pthread_create(thread_pool->ptid + i, NULL, thread_func, (void *)thread_pool);
    }
}
void sig_func(int signum)
{
    printf("sig_func\n");
    int flag_exit = 1;
    int ret = write(exit_pipe[1], &flag_exit, sizeof(int));
    ERROR_CHECK(ret, -1, "write");
}
void destroy_thread_pool(Thread_Pool *thread_pool)
{
    free(thread_pool->ptid);
    int ret = pthread_mutex_destroy(&thread_pool->tqueue.mutex);
    PTHREAD_ERROR_CHECK(ret, "pthread_mutex_destroy");
    ret = pthread_cond_destroy(&thread_pool->tqueue.cond);
    PTHREAD_ERROR_CHECK(ret, "pthread_cond_destroy");
}